#!/bin/bash

# 定义版本号
DOCKER_VERSION=25.0.2
DOCKER_COMPOSE_VERSION=v2.24.5

#定义版本
TAG="7.3.6"

HOST_IP=localhost

#配置mysql数据库root用户密码
MYSQL_PW=Redxun2022Mysql

#配置redis密码
REDIS_PW=Redxun2022redis

# 设置daemon.json文件的路径
DAEMON_JSON_PATH="/etc/docker/daemon.json"

BASE_DIR=$(dirname $0)/..

echo "当前目录......"
echo $BASE_DIR

echo "转换linux文件格式......"
#find . -type f -print0 | xargs -0 sed -i "s/\r//g"
sed -i 's/\r//' build.sh

# 判断是不是 root 用户，非 root 用户无法执行安装
if [ $USER != "root" ]
then
    echo "ERROR: Unable to perform installation as non-root user."
    exit
fi


# 查找空间最大目录
df_lines=$(df -P | wc -l)
max_dir=''
max_siz=0
for i in $(seq 2 $df_lines)
do
    temp_siz=$(df -P | awk -v i=$i 'NR==i{print $4}')
    if [ $temp_siz -gt $max_siz ];then
        max_siz=$temp_siz
        max_dir=$(df -P | awk -v i=$i 'NR==i{print $NF}')
    fi
done
echo "当前磁盘最大目录......"
echo $max_dir

echo "定义docker目录......"
mkdir -p $max_dir/docker
mkdir -p $max_dir/docker/tmp

echo "配置目录权限......"
chown -R 5000:5000 upload logs
chmod -R 777 *

chmod 644 mysql/conf/my.cnf


# 检查.env文件是否存在
if [ -f .env ]; then
  echo ".env file exists."
else
    cat > .env << EOF
REGISTER=
TAG=$TAG
HOST_IP=$HOST_IP
MYSQL_PW=$MYSQL_PW
REDIS_PW=$REDIS_PW
EOF

fi


echo "检查docker环境......"
docker -v
if [[ $? -eq 0 ]]; then
  echo "检查到docker已经安装! 跳过安装步骤"
else
    # 生成 daemon.json 配置文件
    echo "进行docker配置......"
    # 使用test命令检查文件是否存在
    if test -f "$DAEMON_JSON_PATH"; then
       echo "daemon.json 文件存在。"
    else
      echo "daemon.json 文件不存在。"
      mkdir -p /etc/docker/
  cat > /etc/docker/daemon.json << EOF
  {
      "data-root": "$max_dir/docker",
      "registry-mirrors": ["https://docker.m.daocloud.io",
      "https://dockerproxy.com","https://docker.mirrors.ustc.edu.cn",
      "https://docker.nju.edu.cn",
      "https://registry.docker-cn.com",
      "http://dev.redxun.cn:20080",
      "https://docker.registry.cyou",
      "https://docker-cf.registry.cyou",
      "https://dockercf.jsdelivr.fyi",
      "https://docker.jsdelivr.fyi",
      "https://dockertest.jsdelivr.fyi",
      "https://mirror.aliyuncs.com",
      "https://dockerproxy.com",
      "https://mirror.baidubce.com",
      "https://docker.m.daocloud.io",
      "https://docker.nju.edu.cn",
      "https://docker.mirrors.sjtug.sjtu.edu.cn",
      "https://docker.mirrors.ustc.edu.cn",
      "https://mirror.iscas.ac.cn",
      "https://docker.rainbond.cc"],
      "insecure-registries":["dev.redxun.cn:20080"]
  }
EOF

    fi
  # 安装 docker
  # curl -O https://download.docker.com/linux/static/stable/$(uname -m)/docker-${DOCKER_VERSION}.tgz
  tar -zxvf docker-${DOCKER_VERSION}.tgz
  chmod +x docker/*
  mv docker/* /usr/bin
  dockerd --version

  # 检查 docker 是否安装成功
  if [ $? -ne 0 ]
  then
      echo "ERROR: docker install failed."
      rm -rf docker docker-${DOCKER_VERSION}.tgz
      exit
  fi
  # rm -rf docker docker-${DOCKER_VERSION}.tgz

  # 启动 dockerd
  nohup dockerd > /var/log/dockerd.log 2>&1 &

  # 检查 dockerd 是否启动成功
  pidof dockerd
  if [ $? -ne 0 ]
  then
      echo "ERROR: dockerd failed to start."
      exit
  fi

  # 设置 dockerd 开机自启动
  echo "nohup dockerd > /var/log/dockerd.log 2>&1 &" >> /etc/rc.local
  chmod +x /etc/rc.local

fi

if which docker-compose >/dev/null; then
 echo "检测到 Docker Compose 已安装，跳过安装步骤"
 docker-compose -v
else
  # 安装 docker-compose
  #curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 2>&1 | tee -a ${CURRENT_DIR}/install.log
  cp docker-compose-${DOCKER_COMPOSE_VERSION}-linux-x86_64 /usr/local/bin/docker-compose
  chmod +x /usr/local/bin/docker-compose
  ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
 docker-compose -version
 echo "... 在线安装 docker-compose 成功"
  # 检查 docker compose 是否安装成功
  if [ $? -ne 0 ]
  then
      echo "ERROR: docker compose install failed."
      exit
  fi
fi



function all() {

    echo "ac-boot镜像构建......cmd:(docker build -t ac-boot:${TAG} .)"
    cd api
    docker build -t ac-boot:${TAG} .

    cd ..
}

case "$1" in
  api)
      echo "ac-boot镜像构建......cmd:(docker build -t ac-boot:${TAG} .)"
      cd api
      docker build -t ac-boot:${TAG} .
      cd ..
      ;;

  all)
        all
        ;;
   *)
  echo $"Usage: $0 {all|api}"
  RETVAL=1
esac
exit $RETVAL

echo "构建后的docker images镜像列表:"
docker images

echo "当前正在运行的Docker容器实例列表:"
docker ps

